package com.memory.wechat.domain.util;

import com.memory.wechat.domain.msg.MessageType;
import com.memory.wechat.domain.response.Articles;
import com.memory.wechat.domain.response.NewsOutputMessage;
import com.memory.wechat.domain.response.TextMessage;

public class ReplyMessageUtil {
    /**
     * <xml> <ToUserName><![CDATA[toUser]]></ToUserName>
     * <FromUserName><![CDATA[fromUser]]></FromUserName>
     * <CreateTime>12345678</CreateTime> <MsgType><![CDATA[news]]></MsgType>
     * <ArticleCount>2</ArticleCount> <Articles> <item>
     * <Title><![CDATA[title1]]></Title>
     * <Description><![CDATA[description1]]></Description>
     * <PicUrl><![CDATA[picurl]]></PicUrl> <Url><![CDATA[url]]></Url> </item>
     * <item> <Title><![CDATA[title]]></Title>
     * <Description><![CDATA[description]]></Description>
     * <PicUrl><![CDATA[picurl]]></PicUrl> <Url><![CDATA[url]]></Url> </item>
     * </Articles> </xml>
     *
     * @Title sendImageTextMessage
     * @Description 回复图文消息
     * @param message
     * @return
     */
    public static String sendImageTextMessage(NewsOutputMessage message) {
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        sb.append("<ToUserName><![CDATA[" + message.getToUserName() + "]]></ToUserName>");
        sb.append("<FromUserName><![CDATA[" + message.getFromUserName() + "]]></FromUserName>");
        sb.append("<CreateTime>" + message.getCreateTime() + "</CreateTime>");
        sb.append("<MsgType><![CDATA[" + MessageType.IMAGE_TEXT_MESSAGE + "]]></MsgType>");
        sb.append("<ArticleCount>" + message.getArticleCount() + "</ArticleCount>");
        sb.append("<Articles> ");
        for (Articles article : message.getArticles()) {
            sb.append("<item>");
            if(article.getTitle()!=null && article.getTitle()!=""){
                sb.append("<Title><![CDATA[").append(article.getTitle()).append("]]></Title>");
            }
            if(article.getDescription()!=null && article.getDescription()!=""){
                sb.append("<Description><![CDATA[").append(article.getDescription()).append("]]></Description>");
            }
            if(article.getPicUrl()!=null && article.getPicUrl()!=""){
                sb.append("<PicUrl><![CDATA[").append(article.getPicUrl()).append("]]></PicUrl>");
            }
            if(article.getUrl()!=null && article.getUrl()!=""){
                sb.append("<Url><![CDATA[").append(article.getUrl()).append("]]></Url>");
            }
            sb.append("</item>");
        }
        sb.append("</Articles>");
        sb.append("</xml>");
        return sb.toString();
    }


    /**
     * <xml> <ToUserName>< ![CDATA[toUser] ]></ToUserName> <FromUserName><
     * ![CDATA[fromUser] ]></FromUserName> <CreateTime>12345678</CreateTime>
     * <MsgType>< ![CDATA[text] ]></MsgType> <Content>< ![CDATA[你好] ]></Content>
     * </xml> sendTextMessage
     * @param message
     * @return
     */
    public static String sendTextMessage(TextMessage message) {
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        sb.append("<ToUserName><![CDATA[" + message.getToUserName() + "]]></ToUserName>");
        sb.append("<FromUserName><![CDATA[" + message.getFromUserName() + "]]></FromUserName>");
        sb.append("<CreateTime>" + message.getCreateTime() + "</CreateTime>");
        sb.append("<MsgType><![CDATA[" + MessageType.TEXT_MESSAGE + "]]></MsgType>");
        sb.append("<Content><![CDATA[" + message.getContent() + "]]></Content>");
        sb.append("</xml>");
        return sb.toString();
    }
}
